package com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.liuyushan.mvp_lib.View.BaseFragment;
import com.bw.liuyushan.zhuangao4_mvptest1.Adapter.Fragment1Adapter;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Contrack.ICTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Activity.UserActivity;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.TableLayout.TabFragment1;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.TableLayout.TabFragment2;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment.TableLayout.TabFragment3;
import com.bw.liuyushan.zhuangao4_mvptest1.R;
import com.frame.lib_common.slidingmenu.SlidingMenu;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import static android.content.Context.MODE_PRIVATE;

/**
 * ZhuanGao4_MVPTest1
 * file name is : Fragment_Main1
 * created by Ender on 2021/8/11 14:47.
 * author : Liuyushan
 * Describe:Fragment类 也就是V层 向P层要数据 进行显示
 */
public class Fragment_Main1 extends BaseFragment<BasePresenterTest> implements ICTest.C_ViewTest {
    private TabLayout tabFgm1;
    private ViewPager vpF1;
    ArrayList<Fragment> fragments1;//嵌套fragment的数组
    ArrayList<String> strings;//tablelayout的数组
    private TextView LoginPage;
    SharedPreferences sharedPreferences;//sp对象
//    private NavigationView naView;
    private ImageView imggg;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //EventBus的注册
        EventBus.getDefault().register(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_main1;
    }

    @Override
    public void initView() {
        //防止内存泄露
        ip = new BasePresenterTest(new BaseModelTest(), this);

        tabFgm1 = (TabLayout) findViewById(R.id.tab_Fgm1);
        vpF1 = (ViewPager) findViewById(R.id.vp_F1);
        LoginPage = (TextView) findViewById(R.id.Login_page);
//        naView = (NavigationView) findViewById(R.id.naView);
        imggg = (ImageView) findViewById(R.id.imggg);
    }

    @Override
    public void initDate() {
        //创建sp对象->全局变量
        sharedPreferences = getActivity().getSharedPreferences("sp", MODE_PRIVATE);

//        String string = sharedPreferences.getString("LoginUser", "登 录");
//        LoginPage.setText(string);

        /**
         * fragment的初始化
         */
        fragments1 = new ArrayList<>();
        fragments1.add(new TabFragment1());
        fragments1.add(new TabFragment2());
        fragments1.add(new TabFragment3());

        strings = new ArrayList<>();
        strings.add("热点");
        strings.add("推荐");
        strings.add("发现");
        //设置适配器
        Fragment1Adapter fragment1Adapter = new Fragment1Adapter(getChildFragmentManager(), fragments1, strings);
        vpF1.setAdapter(fragment1Adapter);
        tabFgm1.setupWithViewPager(vpF1);
        /**
         * sp接收布尔值->判断是否登录
         * 登录成功->隐藏登录->显示侧拉按钮
         */
        boolean isLogin = sharedPreferences.getBoolean("isLogin", false);
        if (isLogin) {
            LoginPage.setVisibility(View.GONE);//隐藏且不占位
            imggg.setVisibility(View.VISIBLE);//显示
        }
        /**
         * 登录成功状态下->显示控件imgg
         * 点击事件->发送粘性事件->等待MainActivity接收后弹出侧拉菜单
         */
        imggg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送事件
                EventBus.getDefault().post("个人信息表");
            }
        });
        /**
         * 未登录状态下->显示控件LoginPage
         * 点击事件->跳转UserActivity->去进行登录注册
         */
        LoginPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("请先注册");
                Intent intent = new Intent(getActivity(), UserActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 接收MainActivity发送的粘性事件
     * 退出登录状态->清空sp->接收信息->判断sp
     * 退出登录后恢复之前的状态
     * @param re
     */
    //接收粘性事件 ThreadMode.MAIN接收方法在主线程进程
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void EventBusExit(String re){
        if (re.equals("退出登录")){
            boolean isLogin = sharedPreferences.getBoolean("isLogin", false);
            if (!isLogin) {//!!!sp的值已经清空了 所以这里直接为defValue也就是false 要判断不等于isLogin
                LoginPage.setVisibility(View.VISIBLE);//显示
                imggg.setVisibility(View.GONE);//隐藏且不占位
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //EventBus的注销
        EventBus.getDefault().unregister(this);
    }
}
